package de.tabuma.pm.gadget.view;
import de.tabuma.lib.tabumaview;
import de.tabuma.pm.*;
import de.tabuma.pm.gadget.entity.*;
import de.tabuma.pm.gadget.logik.*;
import de.tabuma.pm.externalproject.entity.*;
import de.tabuma.pm.externalproject.logik.*;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.*;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author alefen
 */
public class settings extends tabumaview {

    private static final Logger log = Logger.getLogger(settings.class.getName());

    public settings(String servlet, Hashtable hashtable) {
        super(servlet, hashtable);
    }
    @Override
    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        String gadget_setting_action = (String) request.getAttribute(constants.GADGET_ACTION_SETTING_ACTION);
        if(gadget_setting_action==null) {
            gadget_setting_action = "0";
        }
        if(gadget_setting_action.equals("null")) {
            gadget_setting_action = "0";
        }
        int gadget_setting_action_int = Integer.valueOf(gadget_setting_action);

        Gadget gadget = (Gadget) request.getAttribute(constants.gadget);

        if(gadget_setting_action_int==0) {
            gadget_setting_action_int = gadget.getNavMode();
        }

        String soapContent = "soap content";
        String externalContent = "external content";
        String htmlContent = "html content";
        String cancel = "cancel";
        
        String soapText = "Soap Content";
        String externalUrl = "Url";
        String externalLogin = "Login Url";
        String save = "save";

        String minute = "minute(s)";

        String minutes = "update time";

        String text = "settings for {1}";
        String noLoginNeeded = "no login needed";

        out.println("<div class=\"contentContent\">");
        out.println("<div class=\"contentBoxed\">");
        out.println("<div class=\"contentHeader\">" + text.replaceAll("\\{1\\}",gadget.getName()) + "</div>");
        out.println("<div class=\"contentContent\">");

        List<ExternalProject> eList = (List<ExternalProject>) request.getAttribute(constants.externalProjectList);

        String soapLink = this.getServlet() + "?" + this.getLinkAsAttribute() + "&amp;" + constants.GADGET_ACTION_SETTING_ACTION + "=" + constants.GADGET_ACTION_SETTING_SOAP_CONTENT + "&amp;" + constants.gadgetId + "=" + gadget.getId() + "#" + constants.gadgetA;
        String externalLink = this.getServlet() + "?" + this.getLinkAsAttribute() + "&amp;" + constants.GADGET_ACTION_SETTING_ACTION + "=" + constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT + "&amp;" + constants.gadgetId + "=" + gadget.getId() + "#" + constants.gadgetA;
        String htmlLink = this.getServlet() + "?" + this.getLinkAsAttribute() + "&amp;" + constants.GADGET_ACTION_SETTING_ACTION + "=" + constants.GADGET_ACTION_SETTING_HTML_CONTENT + "&amp;" + constants.gadgetId + "=" + gadget.getId() + "#" + constants.gadgetA;

        switch(gadget_setting_action_int) {
            case constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT:
                out.println("<div class=\"tab\">");
                out.println("<div class=\"tabelementbeginnclose\"></div>");
                out.println("<div class=\"tabelementclose\"><a href=\""+soapLink+"\">"+soapContent+"</a></div>");
                out.println("<div class=\"tabelementopen\">"+externalContent+"</div>");
                out.println("<div class=\"tabelementclose\"><a href=\""+htmlLink+"\">"+htmlContent+"</a></div>");
                out.println("<div class=\"tabelementclose\"></div>");
                out.println("</div>");
                break;
            default:
                out.println("<div class=\"tab\">");
                out.println("<div class=\"tabelementbeginnclose\"></div>");
                out.println("<div class=\"tabelementclose\"><a href=\""+soapLink+"\">"+soapContent+"</a></div>");
                out.println("<div class=\"tabelementclose\"><a href=\""+externalLink+"\">"+externalContent+"</a></div>");
                out.println("<div class=\"tabelementopen\">"+htmlContent+"</div>");
                out.println("<div class=\"tabelementclose\"></div>");
                out.println("</div>");
                break;
        }

        switch(gadget_setting_action_int) {
            case constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT:
                String externalProjectId = (String) request.getAttribute(constants.externalProjectId);
                if(externalProjectId==null) {
                    externalProjectId = "0";
                }
                int externalProjectIdInt = Integer.valueOf(externalProjectId).intValue();
                out.println(getFormBeginn(constants.gadgetA));
                out.println(getHiddenAsAttribute());
                out.println("<input type=\"hidden\" name=\""+constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT_UPDATAE+"\" value=\"1\"/>");
                out.println("<p>" + externalLogin + " <select name=\""+constants.externalProjectId+"\">");
                out.println("<option value=\"0\">"+noLoginNeeded+"</option>");
                for(int i=0;i<eList.size();i++) {
                    ExternalProject eProject = eList.get(i);
                    if(eProject.getId().intValue()==externalProjectIdInt) {
                        out.println("<option value=\""+eProject.getId()+"\" selected>"+eProject.getName() + "</option>");
                    }
                    else {
                        out.println("<option value=\""+eProject.getId()+"\">"+eProject.getName() + "</option>");
                    }
                }
                out.println("</select></p>");
                out.println("<input type=\"hidden\" name=\""+constants.gadgetId+"\" value=\""+gadget.getId().intValue()+"\"/>");
                String url = (String) request.getAttribute(constants.externalContentUrl);
                if(url==null) {
                    url = "";
                }
                out.println("<p>" + externalUrl + " <input type=\"text\" name=\""+constants.externalContentUrl+"\" value=\""+url+"\"/></p>");
                out.println("<p>" + minutes);
                out.println("<select name=\"" + constants.GADGET_MINUTES + "\">");
                for(int i=1;i<101;i++) {
                    if(i==gadget.getUpdateTime()) {
                        out.println("<option value=\""+i+"\" selected>" + i + " " + minute +"</option>");
                    }
                    else {
                        out.println("<option value=\""+i+"\">" + i + " " + minute +"</option>");
                    }
                }
                out.println("</select>");
                out.println("</p>");
                out.println("<input type=\"submit\" class=\"button\" name=\"submit\" value=\"" + save + "\"/>");
                out.println(getFormEnd());
                break;
            default:
                out.println("<div class=\"wysiwygeditor\">");
                out.println("<script type=\"text/javascript\" src=\"openwysiwyg/wysiwyg.js\"></script>");
                out.println(getFormBeginn(constants.gadgetA));
                out.println(getHiddenAsAttribute());
                out.println("<input type=\"hidden\" name=\""+constants.GADGET_ACTION_SETTING_HTML_CONTENT_UPDATE+"\" value=\"1\"/>");
                String text1 = (String) request.getAttribute(constants.HtmlContentTextareaName);
                if(text1==null) {
                    text1 = "";
                }
                out.println("<textarea name=\""+constants.HtmlContentTextareaName+"\" id=\""+constants.HtmlContentTextareaName+"\" cols=\"40\" rows=\"3\">");
                out.println(text1);
                out.println("</textarea>");
                out.println("<script type=\"text/javascript\">");
                out.println("generate_wysiwyg('"+constants.HtmlContentTextareaName+"');");
                out.println("</script>");
                out.println("<input type=\"hidden\" name=\""+constants.gadgetId+"\" value=\""+gadget.getId().intValue()+"\"/>");
                out.println("<input type=\"submit\" class=\"button\" name=\"submit\" value=\"" + save + "\"/>");
                out.println(getFormEnd());
                out.println("</div>");
                break;
        }

        out.println(getFormBeginn(constants.gadgetA));
        out.println(getHiddenAsAttribute());
        out.println("<input type=\"submit\" class=\"button\" name=\""+constants.cancel+"\" value=\"" + cancel + "\"/>");
        out.println(getFormEnd());
        
        out.println("</div>");
        out.println("</div>");
        out.println("</div>");
    }
}